import javax.swing.JOptionPane;

public class Ejercicio6 {

	public static void main(String[] args) {

		
		String letras[] = { "M", "CM", "D", "CD", "C", "XC", "L", "XL", "X",
				"IX", "V", "IV", "I" };
		int dec[] = { 1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1 };

		do {
			int n = -1, i = 0;
			boolean ok;
			do {
				try {
					ok = true;
					n = Integer.parseInt(JOptionPane
							.showInputDialog("ingrese numero"));

					if (n == 0) {
						System.exit(0);
					}

					if (n < 0 || n >= 3500) {
						ok = false;
					}

				} catch (Exception e) {
					ok = false;
				}

				if (ok == false) {
					JOptionPane.showMessageDialog(null,
							"Caracter invalido, intente nuevamente", "ERROR",
							JOptionPane.ERROR_MESSAGE);
				}
			} while (ok == false);

			while (n != 0) {
				System.out.printf("%-4d ", n);
				while (n > 0) {
					if (n >= dec[i]) {
						System.out.print(letras[i]);
						n = n - dec[i];
					} else {
						i++;
					}
				}
			}
		
		}while(letras[0].equals("M"));

	}

}
